﻿using System.Collections.Generic;
using Windows.UI.ViewManagement;
using RssReader.Common;
using RssReader.Feeds;
using Windows.UI.ApplicationSettings;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Item = RssReader.FeedProvider.Item;

// The Items Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234233

namespace RssReader
{
    /// <summary>
    /// A page that displays a collection of item previews.  In the Split Application this page
    /// is used to display and select one of the available groups.
    /// </summary>
    public sealed partial class ItemsPage : Page
    {
        private readonly UrlHelper _urlHelper = new UrlHelper();
        private readonly CategoryHelper _categoryHelper = new CategoryHelper();
        private NavigationHelper navigationHelper;
        private ObservableDictionary defaultViewModel = new ObservableDictionary();

        public NavigationHelper NavigationHelper
        {
            get { return this.navigationHelper; }
        }

        public ObservableDictionary DefaultViewModel
        {
            get { return this.defaultViewModel; }
        }
        public object Item_height_for_screen_resolution { get; set; }

        public ItemsPage()
        {
            this.InitializeComponent();
            this.navigationHelper = new NavigationHelper(this);
            this.navigationHelper.LoadState += navigationHelper_LoadState;
            SettingsPane.GetForCurrentView().CommandsRequested += App.SettingsCommandHelper.On_commands_requested;
            NavigationCacheMode = NavigationCacheMode.Enabled;
            Window.Current.SizeChanged += (s, e) => UpdateVisualState();
        }

        private async void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
        {
            var allItems = e.NavigationParameter as IList<Item>;

            MainGrid.Background = new SolidColorBrush(App.ARGBColor);

            if (allItems == null)
                Get_feed();
            else
            {
                itemGridView.ItemsSource = allItems;
                itemListView.ItemsSource = allItems;
            }
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            navigationHelper.OnNavigatedTo(e);
            UpdateVisualState();
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            navigationHelper.OnNavigatedFrom(e);
            UpdateVisualState();
        }

        private void UpdateVisualState()
        {
            ApplicationView currentAppView = ApplicationView.GetForCurrentView();

            if (currentAppView.AdjacentToLeftDisplayEdge)
            {
                VisualStateManager.GoToState(this, "Portrait", false);
            }
            if (currentAppView.AdjacentToRightDisplayEdge)
            {
                VisualStateManager.GoToState(this, "Portrait", false);
            }
            if (currentAppView.IsFullScreen)
            {
                VisualStateManager.GoToState(this, "Landscape", false);
            }
            if (currentAppView.Orientation == ApplicationViewOrientation.Portrait)
            {
                VisualStateManager.GoToState(this, "Portrait", false);
            } 
           
        }


        private async void Get_feed()
        {
            ring.IsActive = true;
            
            var allItems = await _categoryHelper.Get_all_blog_posts_from_shown_categories();
            itemGridView.ItemsSource = allItems;
            itemListView.ItemsSource = allItems;
            ring.IsActive = false;
        }

        private void Item_view_item_click(object sender, ItemClickEventArgs e)
        {
            var itemDetailsViewHelper = new ItemDetailsViewHelper().Get_selected_item_and_show_item_details_view(e, App.ItemsKeyString, Window.Current.Content);
            this.Frame.Navigate(typeof(ItemDetailPage), itemDetailsViewHelper);
        }

        private void Categories_view_click(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(GroupedItemsPage));
        }
    }
}
